package com.goliathonline.android.kegbot;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;

public class Util {
	
	private JSONObject _json;

	@SuppressWarnings("unchecked")
	private Hashtable ProtoToDict(Kegnet.FlowRequest message)
	{
		Hashtable ret = new Hashtable();
		if(!message.isInitialized())
			return ret;
		Map<FieldDescriptor, Object> mp = message.getAllFields();
		//Get Map in Set interface to get key and value
        Set s=mp.entrySet();

        //Move next key and value of Map by iterator
        Iterator it=s.iterator();

        while(it.hasNext())
        {
            // key=value separator this by Map.Entry to get key and value
            Map.Entry m =(Map.Entry)it.next();
            
            // getKey is used to get key of Map
            FieldDescriptor key = (FieldDescriptor)m.getKey();

            if(key.getType() == FieldDescriptor.Type.ENUM)
            	ret.put(key.getName(), Kegnet.FlowRequest.Action.valueOf((EnumValueDescriptor)m.getValue()));
            else
            	ret.put(key.getName(), m.getValue());
        }
        return ret;
	}
	
	@SuppressWarnings("unchecked")
	private Hashtable ProtoToDict(Kegnet.DrinkCreatedEvent message)
	{
		Hashtable ret = new Hashtable();
		if(!message.isInitialized())
			return ret;
		Map<FieldDescriptor, Object> mp = message.getAllFields();
		//Get Map in Set interface to get key and value
        Set s=mp.entrySet();

        //Move next key and value of Map by iterator
        Iterator it=s.iterator();

        while(it.hasNext())
        {
            // key=value separator this by Map.Entry to get key and value
            Map.Entry m =(Map.Entry)it.next();
            
            // getKey is used to get key of Map
            FieldDescriptor key = (FieldDescriptor)m.getKey();

            ret.put(key.getName(), m.getValue());
        }
        return ret;
	}
	
	@SuppressWarnings("unchecked")
	private Hashtable ProtoToDict(Kegnet.TokenAuthEvent message)
	{
		Hashtable ret = new Hashtable();
		if(!message.isInitialized())
			return ret;
		Map<FieldDescriptor, Object> mp = message.getAllFields();
		//Get Map in Set interface to get key and value
        Set s=mp.entrySet();

        //Move next key and value of Map by iterator
        Iterator it=s.iterator();

        while(it.hasNext())
        {
            // key=value separator this by Map.Entry to get key and value
            Map.Entry m =(Map.Entry)it.next();
            
            // getKey is used to get key of Map
            FieldDescriptor key = (FieldDescriptor)m.getKey();

            if(key.getType() == FieldDescriptor.Type.ENUM)
            	ret.put(key.getName(), Kegnet.TokenAuthEvent.TokenState.valueOf((EnumValueDescriptor)m.getValue()));
            else
            	ret.put(key.getName(), m.getValue());
        }
        return ret;
	}
	
	@SuppressWarnings("unchecked")
	private Hashtable ProtoToDict(Kegnet.ThermoEvent message)
	{
		Hashtable ret = new Hashtable();
		if(!message.isInitialized())
			return ret;
		Map<FieldDescriptor, Object> mp = message.getAllFields();
		//Get Map in Set interface to get key and value
        Set s=mp.entrySet();

        //Move next key and value of Map by iterator
        Iterator it=s.iterator();

        while(it.hasNext())
        {
            // key=value separator this by Map.Entry to get key and value
            Map.Entry m =(Map.Entry)it.next();
            
            // getKey is used to get key of Map
            FieldDescriptor key = (FieldDescriptor)m.getKey();

          	ret.put(key.getName(), m.getValue());
        }
        return ret;
	}
	
	@SuppressWarnings("unchecked")
	private Hashtable ProtoToDict(Kegnet.Ping message)
	{
		Hashtable ret = new Hashtable();
		if(!message.isInitialized())
			return ret;
		Map<FieldDescriptor, Object> mp = message.getAllFields();
		//Get Map in Set interface to get key and value
        Set s=mp.entrySet();

        //Move next key and value of Map by iterator
        Iterator it=s.iterator();

        while(it.hasNext())
        {
            // key=value separator this by Map.Entry to get key and value
            Map.Entry m =(Map.Entry)it.next();
            
            // getKey is used to get key of Map
            FieldDescriptor key = (FieldDescriptor)m.getKey();

           	ret.put(key.getName(), m.getValue());
        }
        return ret;
	}
	
	@SuppressWarnings({ "unchecked", "static-access" })
	public String ProtoMessageToJson(Kegnet.FlowRequest message)
	{
		  Hashtable wrapper = new Hashtable();
		  wrapper.put("method", message.getDescriptor().getName());
		  wrapper.put("id", "");
		  wrapper.put("params", ProtoToDict(message));
		  _json = new JSONObject(wrapper);
		  return _json.toString();
	}
	
	@SuppressWarnings({ "unchecked", "static-access" })
	public String ProtoMessageToJson(Kegnet.DrinkCreatedEvent message)
	{
		  Hashtable wrapper = new Hashtable();
		  wrapper.put("method", message.getDescriptor().getName());
		  wrapper.put("id", "");
		  wrapper.put("params", ProtoToDict(message));
		  _json = new JSONObject(wrapper);
		  return _json.toString();
	}
	
	@SuppressWarnings({ "unchecked", "static-access" })
	public String ProtoMessageToJson(Kegnet.TokenAuthEvent message)
	{
		  Hashtable wrapper = new Hashtable();
		  wrapper.put("method", message.getDescriptor().getName());
		  wrapper.put("id", "");
		  wrapper.put("params", ProtoToDict(message));
		  _json = new JSONObject(wrapper);
		  return _json.toString();
	}
	
	@SuppressWarnings({ "unchecked", "static-access" })
	public String ProtoMessageToJson(Kegnet.ThermoEvent message)
	{
		  Hashtable wrapper = new Hashtable();
		  wrapper.put("method", message.getDescriptor().getName());
		  wrapper.put("id", "");
		  wrapper.put("params", ProtoToDict(message));
		  _json = new JSONObject(wrapper);
		  return _json.toString();
	}
	
	@SuppressWarnings({ "unchecked", "static-access" })
	public String ProtoMessageToJson(Kegnet.Ping message)
	{
		  Hashtable wrapper = new Hashtable();
		  wrapper.put("method", message.getDescriptor().getName());
		  wrapper.put("id", "");
		  wrapper.put("params", ProtoToDict(message));
		  _json = new JSONObject(wrapper);
		  return _json.toString();
	}
	
	public JSONObject StringToJson(String message)
	{
		JSONObject json;
		try {
			json = new JSONObject(message);
		} catch (JSONException e) {
			// make a blank object
			json = new JSONObject();
		}
		return json;
	}
	
	public String JsonToProto(JSONObject json)
	{
		try {
			String method = (String)json.get("method");
			return method;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			return e.toString();
		}
	}
	
	public double MillToOz(Double mills)
	{
		return mills * 0.0338140227;
	}
}
